﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using System.Data;
using IFRS_Global;
using System.Threading;
using System.Globalization;

public partial class IFRS_WIN19_2E : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.ViewMenu();
            this.SetErrorMessage();
            this.BindDDL();
            this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }
    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN19_1S.aspx");
    }

    #endregion
    #region "----- Methods -------"
    private void ViewMenu()
    {
        this.MenuTitleBar = Resources.ifrsMenu.WIN19_2E;
    }
    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        reqTransactionDate.SetRequiredField(lblTransactionDate, txtTransactionDate, btnSAVE);
        reqSource_data.SetRequiredField(lblSource_data, ddlSource_data, btnSAVE);
        reqAccountNo.SetRequiredField(lblAccountNo, txtAccountNo, btnSAVE);
        reqEIRValue.SetRequiredField(lblEIRValue, txtEIRValue, btnSAVE);
    }
    private void BindDDL()
    {
        ddlSource_data.SetData(DDL_TYPE.SourceData, "", false, true);
    }
    private void BindData(int ID)
    {
        var vREIR = new SPR_EIR();
        DataTable dtvREIR = vREIR.SEARCH_BYID(ID);
        List<EIR> EIR = new List<EIR>();
        ConvertHelper.ConvertDataTableToObject(dtvREIR, EIR);

        if (dtvREIR.Rows.Count > 0)
        {
            hidEIRID.Value = EIR[0].EIRID.ToString();
            txtAccountNo.Text = EIR[0].AccountNo;
            txtTransactionDate.Text = EIR[0].TransactionDate.DateToString();
            txtEIRValue.Text = EIR[0].EIRValue.ToString();
            ddlSource_data.SelectedValue = EIR[0].SourceDataID.ToString();

        }
    }

    private void SaveData()
    {

        try
        {
            EIR vEIR = new EIR();
            vEIR.EIRID = hidEIRID.Value.AsInt32();
            vEIR.AccountNo = txtAccountNo.Text;
            vEIR.TransactionDate = txtTransactionDate.Text.StringToDateAllowNull();
            vEIR.SourceDataID = ddlSource_data.SelectedValue.AsInt32();
            vEIR.EIRValue = txtEIRValue.Text.AsDecimal();
            vEIR.SetStandardField();

            var SPR_EIR = new SPR_EIR();
            int iID = SPR_EIR.UPDATE(vEIR);
            if (iID > 0)
            {
                // Utility.AlertMessage(Page, Resources.ifrsResource.SaveData);
                this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN19_2E.aspx?&id={0}", hidEIRID.Value.AsInt32())));
                //Response.Redirect(string.Format("~/IFRS/SEC01_2E.aspx?id={0}", iID)); 
            }
            else
            {
                Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    #endregion
}